package com.lf.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.lf.DAO.studentDAO;
import com.lf.DAO.teacherDAO;
import com.lf.entity.shiti.students;
import com.lf.entity.shiti.teacher;
import com.lf.impl.stu_impl;
import com.lf.impl.tea_impl;

/**
 * Servlet implementation class students_Servlet
 */
@WebServlet("/students_Servlet")
public class students_Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public students_Servlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");;
		response.setContentType("text/html;charset=utf-8");
		String action = request.getParameter("action");
		if(action==null){
			findAll(request,response);
		}
		switch (action) {
		case "findAll":
			findAll(request,response);
			break;
		case "find_student_name":
			find_student_name(request,response);
			break;
		case"add_stu":
			add_stu(request,response);
			break;
		case"update_stu":
			update_stu(request,response);
			break;
		case"delect_stu":
			delect_stu(request,response);
			break;
		case"login":
			login(request,response);
			break;
		default:
			break;
		}
	}
	private void findAll(HttpServletRequest request, HttpServletResponse response) throws IOException{
		studentDAO sDao = new stu_impl();
		List<students> list = sDao.findAll();
		request.getSession().setAttribute("list", list);
		response.sendRedirect("test01.jsp");
	}
	
	/**
	 * 根据id查询学生的所有信息
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void find_student_name(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String id = request.getParameter("s_id");
		int s_id = Integer.parseInt(id);
		studentDAO sDao = new stu_impl();
		List<students> list = sDao.find_student_name(s_id);
		request.getSession().setAttribute("list", list);
		response.sendRedirect("students.jsp");
	}
	
	private void add_stu(HttpServletRequest request, HttpServletResponse response) throws IOException{
		
		String old_s_id = new String(request.getParameter("s_id").getBytes(        "ISO-8859-1"), "utf-8");
		int s_id = Integer.valueOf(old_s_id).intValue();
		String s_name = new String(request.getParameter("s_name").getBytes(        "ISO-8859-1"), "utf-8");
		String s_password = new String(request.getParameter("s_password").getBytes(        "ISO-8859-1"), "utf-8");
		String old_s_age = new String(request.getParameter("s_age").getBytes(        "ISO-8859-1"), "utf-8");
		int s_age = Integer.valueOf(old_s_age).intValue();
		String s_address = new String(request.getParameter("s_address").getBytes(        "ISO-8859-1"), "utf-8");
		String s_major = new String(request.getParameter("s_major").getBytes(        "ISO-8859-1"), "utf-8");
		String s_class = new String(request.getParameter("s_class").getBytes(        "ISO-8859-1"), "utf-8");
		studentDAO sDao = new stu_impl();
		int count =sDao.add_stu(s_id, s_name, s_password, s_age, s_address, s_major, s_class);
		if (count>0) {
			System.out.print("成功");
		} else {
			System.out.print("失败");
		}
	}
	private void update_stu(HttpServletRequest request, HttpServletResponse response) throws IOException{
		
		String old_s_id = new String(request.getParameter("s_id").getBytes(        "ISO-8859-1"), "utf-8");
		int s_id = Integer.valueOf(old_s_id).intValue();
		String s_name = new String(request.getParameter("s_name").getBytes(        "ISO-8859-1"), "utf-8");
		String s_password = new String(request.getParameter("s_password").getBytes(        "ISO-8859-1"), "utf-8");
		String old_s_age = new String(request.getParameter("s_age").getBytes(        "ISO-8859-1"), "utf-8");
		int s_age = Integer.valueOf(old_s_age).intValue();
		String s_address = new String(request.getParameter("s_address").getBytes(        "ISO-8859-1"), "utf-8");
		String s_major = new String(request.getParameter("s_major").getBytes(        "ISO-8859-1"), "utf-8");
		String s_class = new String(request.getParameter("s_class").getBytes(        "ISO-8859-1"), "utf-8");
		String old_id = new String(request.getParameter("id").getBytes(        "ISO-8859-1"), "utf-8");
		int id = Integer.valueOf(old_id).intValue();
		studentDAO sDao = new stu_impl();
		int count =sDao.update_stu(s_id, s_name, s_password, s_age, s_address, s_major, s_class, id);
		if (count>0) {
			System.out.print("成功");
		} else {
			System.out.print("失败");
		}
	}
	private void delect_stu(HttpServletRequest request, HttpServletResponse response) throws IOException{
		System.out.print("asdasd");
		studentDAO sDao = new stu_impl();
		String old_s_id = new String(request.getParameter("s_id").getBytes(        "ISO-8859-1"), "utf-8");
		int s_id = Integer.valueOf(old_s_id).intValue();
		int count =sDao.delect_stu(s_id);
		if (count>0) {
			System.out.print("成功");
		} else {
			System.out.print("失败");
		}
	}
	
	/**
	 * 学生登录
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
		String id = request.getParameter("user");
		String password = request.getParameter("password");
		int s_id = Integer.parseInt(id);
		studentDAO sDao = new stu_impl();
		List<students> list = sDao.login(s_id);
		PrintWriter out = response.getWriter();
		if(list.size() == 0){
			System.out.println("账号不存在");
			out.print("<script>alert('账号不存在'); location.href='Login.jsp'</script>");
			out.close();
		}else{
			for(students stu : list){
				if(stu.getS_password().equals(password)){
					System.out.println("登录成功");
					response.sendRedirect("students_Servlet?action=find_student_name&s_id="+s_id);
				}else{
					System.out.println("密码错误");
					out.print("<script>alert('密码错误'); location.href='Login.jsp'</script>");
					out.close();
				}
			}
		}
	}
}
